package com.easygem.controller.business;

import com.easygem.business.service.SysNoticeService;
import com.easygem.common.annotation.ControllerEndpoint;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.business.SysNotice;
import com.easygem.common.response.ResponseBean;
import com.easygem.common.vo.business.SysNoticeVO;
import com.easygem.common.vo.system.PageVO;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author 文晓平
 * @version 1.0
 * @Description 系统通知控制层
 * @date 2024/12/21 0:55
 */
@Api(tags = "系统通知管理")
@RestController
@RequestMapping("/system/sysnotice")
public class SysNoticeController {
    @Autowired
    private SysNoticeService sysNoticeService;
    /**
     * 系统通知列表
     *
     * @return
     */
    @ApiOperation(value = "系统通知列表", notes = "模糊查询系统通知列表")
    @GetMapping("/findList")
    @ResponseBody
    public ResponseBean<PageVO<SysNotice>> findList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                    SysNoticeVO sysNoticeVO) {
        PageVO<SysNotice> sysNoticeList = sysNoticeService.findList(pageNum, pageSize, sysNoticeVO);
        System.out.println("***************************************"+sysNoticeList);
        return ResponseBean.success(sysNoticeList);
    }


    /**
     * 添加系统通知
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加系统通知失败", operation = "添加系统通知")
    @RequiresPermissions({"sysnotice:add"})
    @ApiOperation(value = "添加系统通")
    @PostMapping("/add")
    @ResponseBody
    public ResponseBean add(@RequestBody @Validated SysNoticeVO sysNoticeVO) {
        sysNoticeService.add(sysNoticeVO);
        return ResponseBean.success();
    }

    /**
     * 编辑系统通知
     *
     * @param id 编号
     * @return
     */
    @ApiOperation(value = "编辑系统通知")
    @RequiresPermissions({"sysnotice:edit"})
    @GetMapping("/edit/{id}")
    @ResponseBody
    public ResponseBean edit(@PathVariable Integer id) throws SystemException {
        SysNoticeVO sysNoticeVO = sysNoticeService.edit(id);
        return ResponseBean.success(sysNoticeVO);
    }

    /**
     * 更新系统通知操作
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新系统通知失败", operation = "更新系统通知")
    @ApiOperation(value = "更新系统通知")
    @RequiresPermissions({"sysnotice:update"})
    @PutMapping("/update/{id}")
    @ResponseBody
    public ResponseBean update(@PathVariable Integer id, @RequestBody @Validated SysNoticeVO sysNoticeVO) throws SystemException {
        sysNoticeService.update(id, sysNoticeVO);
        return ResponseBean.success();
    }

    /**
     * 删除系统通知
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除系统通知失败", operation = "删除系统通知")
    @ApiOperation(value = "删除系统通知")
    @RequiresPermissions({"sysnotice:delete"})
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResponseBean delete(@PathVariable Integer id) throws SystemException {
        sysNoticeService.delete(id);
        return ResponseBean.success();
    }


    /**
     * 多选删除系统通知
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除系统通知失败", operation = "删除系统通知")
    @ApiOperation(value = "删除系统通知")
    @RequiresPermissions({"sysnotice:delete"})
    @PostMapping("/delmore")
    @ResponseBody
    public ResponseBean deleteMore(@RequestBody Map<String,Object> map) throws SystemException {
        sysNoticeService.deleteMore(map);
        return ResponseBean.success();
    }




    /**
     * 导出excel
     * @param response
     */
    @ApiOperation(value = "导出excel", notes = "导出所有系统通知的excel表格")
    @PostMapping("/excel")
    @RequiresPermissions("sysnotice:export")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败",operation = "导出系统通知excel")
    public void export(HttpServletResponse response) {
        List<SysNotice> sysNoticeList = this.sysNoticeService.findAll();
        ExcelKit.$Export(SysNotice.class, response).downXlsx(sysNoticeList, false);
    }

    /**
     * 所有系统通知
     *
     * @return
     */
    @ApiOperation(value = "所有系统通知")
    @GetMapping("/findAll")
    @ResponseBody
    public ResponseBean<List<SysNoticeVO>> findAll() {
        List<SysNoticeVO> sysNoticeVOS = sysNoticeService.findAllVO();
        return ResponseBean.success(sysNoticeVOS);
    }



    /**
     * 所有当前用户需要确定的系统通知
     * @return
     */
    @ApiOperation(value = "所有系统通知")
    @GetMapping("/isRead")
    @ResponseBody
    public ResponseBean<List<SysNoticeVO>> selectUserId() {
        List<SysNoticeVO> sysNoticeVO = sysNoticeService.selectByUserId();
        return ResponseBean.success(sysNoticeVO);
    }

    /**
     * 添加是否已读的系统通知
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加是否已读系统通知失败", operation = "添加是否已读系统通知")
    @ApiOperation(value = "添加是否已读系统通")
    @PostMapping("/addSysNoticeIs/{noticeId}")
    @ResponseBody
    public ResponseBean addIs(@PathVariable Long noticeId) {
        sysNoticeService.addSysNoticeIs(noticeId);
        return ResponseBean.success();
    }
}
